package com.sc.controller;

import com.github.pagehelper.PageInfo;
import com.sc.pojo.Posttype;
import com.sc.service.PosttypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/postType")
public class PosttypeController {
    @Autowired
    PosttypeService pts;
    @RequestMapping("/show")
    public String show(@RequestParam(defaultValue = "1") Integer pageNum, Map m){
        PageInfo<Posttype> pt=pts.show(pageNum,4);
        m.put("pt",pt);
        return "/post/postTypeMgr/list";
    }

    @RequestMapping("/isExit")
    @ResponseBody
    public boolean isExit(String type){
        List<Posttype> list=pts.queryAll();
        boolean b=false;
        for(Posttype pt:list){
            if(pt.getType().equalsIgnoreCase(type)){
                b=true;
            }
        }
        return b;
    }

    @RequestMapping("/add")
    public String add(Posttype pt){
        pts.add(pt);
        return "redirect:/postType/show";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id,Map m){
        Posttype pt=pts.queryOne(id);
        m.put("pt",pt);
        return "/post/postTypeMgr/edit";
    }

    @RequestMapping("/update")
    public String update(Posttype pt){
        pts.update(pt);
        return "redirect:/postType/show";
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        pts.delete(id);
        return "redirect:/postType/show";
    }

   @RequestMapping("/deleteAll")
   public String deleteAll(Integer[] ids){
       if (ids != null) {
           pts.deleteAll(ids);
       }
       pts.deleteAll(ids);
       return "redirect:/postType/show";
   }

    //跳转页面
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "/post/postTypeMgr/add";
    }

}
